<template>
  <div>
    <el-dialog
      title="新增会员"
      class="gl_dialog_TitleLine gl_dl_footer"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="880px"
    >
      <el-scrollbar ref="scroll" v-loading="loading" wrap-class="scrollbar-wrapper gl_dialog_height">
        <el-form ref="selectedMsg" class="gl_form_mini" :model="selectedMsg" :rules="selectedMsg_Rules" label-position="top" label-width="120px" style="padding:0 30px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="selectedMsg.name" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="英文名">
                <el-input v-model="selectedMsg.englishName" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="性别" prop="genderCode">
                <el-select v-model="selectedMsg.genderCode" placeholder="请选择性别" size="mini" style="width: 100%" @change="changeGender">
                  <el-option v-for="item in dict.dictGender" :key="item.code" :label="item.value" :value="item.code" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证件类型" prop="idCardTypeCode">
                <el-select v-model="selectedMsg.idCardTypeCode" style="width:100%" size="mini" placeholder="请选择">
                  <el-option v-for="item in dict.idCardType" :key="item.code" :value="item.code" :label="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证件号码" prop="idCardNumber" :rules="selectedMsg.idCardTypeCode === 'IdCard' ? selectedMsg_Rules.idCardNumber : selectedMsg_Rules.unIdCardNumber">
                <el-input v-model="selectedMsg.idCardNumber" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地点" prop="address">
                <el-input v-model="selectedMsg.address" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="日期" prop="passTheExamDateTime">
                <el-date-picker
                  v-model="selectedMsg.passTheExamDateTime"
                  type="date"
                  range-separator="至"
                  placeholder="选择日期"
                  style="width: 100%;"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  @change="handleTimeChange"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书类型" prop="certificateTypeName">
                <el-input v-model="selectedMsg.certificateTypeName" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证书名称" prop="certificateName">
                <el-input v-model="selectedMsg.certificateName" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书等级" prop="certificateLevel">
                <el-input v-model="selectedMsg.certificateLevel" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证书编号" prop="certificateNumber">
                <el-input v-model="selectedMsg.certificateNumber" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书有效期">
                <el-date-picker
                  v-model="selectedMsg.certificateEndDate"
                  type="date"
                  range-separator="至"
                  placeholder="选择日期"
                  style="width: 100%;"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  @change="handleEndTimeChange"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="考官">
                <el-input v-model="selectedMsg.examinerName" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="考官编号" prop="examinerNumber">
                <el-input v-model="selectedMsg.examinerNumber" maxlength="8" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="培训师">
                <el-input v-model="selectedMsg.trainerName" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训师编号">
                <el-input v-model="selectedMsg.trainerNumber" maxlength="20" placeholder="请输入" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-scrollbar>

      <span slot="footer" class="dialog-footer">
        <!-- 取消按钮 -->
        <el-button size="mini" @click="clickCancel">{{ $t('button.cancel') }}</el-button>
        <!-- 新增的保存按钮 -->
        <el-button type="primary" :loading="submitLoading" size="mini" @click="saveNew()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { SiSTKWTrainAddCentreCertificateInput } from '@/lib/form/train/swaggerForm';
import {
  api_POST_TrainCentreCertificateCentreCertificate // 证书添加
} from '@/extend/api/trainswaggerApi/app';
import validator from '@/lib/validate/index.js';
export default {
  props: {
    // 字典信息
    dict: {
      type: Object,
      default () {
        return {};
      }
    },
    dictAll: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data() {
    return {
      datetimerange: '',
      selectedMsg: new SiSTKWTrainAddCentreCertificateInput(),
      selectedMsg_Rules: { // 必填项，验证密码格式
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        genderName: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        genderCode: [{ required: true, message: '性别不能为空', trigger: 'change' }],
        passTheExamDateTime: [{ required: true, message: '日期不能为空', trigger: 'change' }],
        address: [{ required: true, message: '地点不能为空', trigger: 'blur' }],
        certificateTypeName: [{ required: true, message: '证书类型不能为空', trigger: 'blur' }],
        certificateName: [{ required: true, message: '证书名称不能为空', trigger: 'blur' }],
        certificateLevel: [{ required: true, message: '证书等级不能为空', trigger: 'blur' }],
        certificateNumber: [
          { required: true, message: '证书编号不能为空', trigger: 'blur' },
          {
            validator(rule, value, callback) {
              callback(validator.checkDisciplineCode(value, '证书编号格式错误'));
            },
            trigger: 'blur'
          }],
        idCardTypeCode: [{ required: true, message: '证件类型不能为空', trigger: 'blur' }],
        idCardNumber: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
          {
            validator(rule, value, callback) {
              callback(validator.checkIdNumber(value, '身份证号码格式错误'));
            },
            trigger: 'blur'
          }],
        unIdCardNumber: [{ required: true, message: '证件号码不能为空', trigger: 'blur' }],
        trainerNumber: [
          {
            validator(rule, value, callback) {
              callback(validator.checkDisciplineCode(value, '培训师编号格式错误'));
            },
            trigger: 'blur'
          }],
        examinerNumber: [
          {
            validator(rule, value, callback) {
              callback(validator.checkDisciplineCode(value, '考官编号格式错误'));
            },
            trigger: 'blur'
          }]
      },
      loading: false,
      submitLoading: false,
      dialogVisible: false,
      editionList: [],
      cascaderKey: null, // 用于重置级联选择器
      type: '', // 弹框状态，是新增和是修改 new 是新增，edit是修改
      rowId: '' // 编辑时候的id
    };
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.$refs.scroll.wrap.scrollTop = 0;
      }
    }
  },
  mounted() {
    this.$set(this.selectedMsg, 'passTheExamDateTime', '');
  },
  methods: {
    // 地址栏改变
    changeAddressData() {
      this.selectedMsg.countyCode = this.selectedMsg.addressData[0] || '';
      this.selectedMsg.provinceCode = this.selectedMsg.addressData[1] || '';
      this.selectedMsg.cityCode = this.selectedMsg.addressData[2] || '';
    },
    open(row, type) {
      this.loading = true;
      this.selectedMsg = new SiSTKWTrainAddCentreCertificateInput();
      this.$nextTick(() => {
        this.dialogVisible = true;
        this.loading = false;
      });
    },
    // 新增
    saveNew() {
      const that = this;
      this.dict.idCardType.forEach(item => {
        if (item.code === that.selectedMsg.idCardTypeCode) {
          that.selectedMsg.idCardTypeName = item.value;
        }
      });
      this.$refs.selectedMsg.validate(valid => {
        if (valid) {
          this.submitLoading = true;
          api_POST_TrainCentreCertificateCentreCertificate(this.selectedMsg).then(res => {
            this.submitLoading = false;
            this.dialogVisible = false;
            this.$message({ type: 'success', message: '添加成功' });
            this.$parent.getPageList();
          }, error => {
            this.submitLoading = false;
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          });
        } else {
          this.$refs.scroll.wrap.scrollTop = 0;
          this.submitLoading = false;
          return false;
        }
      });
    },
    handleTimeChange(time) {
      this.selectedMsg.passTheExamDateTime = time;
    },
    handleEndTimeChange(time) {
      this.selectedMsg.certificateEndDate = time;
    },
    clickCancel() {
      this.dialogVisible = false;
      this.$set(this.selectedMsg, 'passTheExamDateTime', '');
      this.selectedMsg = new SiSTKWTrainAddCentreCertificateInput();
    },
    changeGender(a) {
      this.dict.dictGender.forEach(item => {
        if (item.code === a) {
          this.selectedMsg.genderName = item.value;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
  .upload_game_img.no_pointer {
    cursor: default;
  }
  .upload_game_img {
    width: 102px;
    height: 96px;
    line-height: 94px;
    text-align: center;
    font-size: 20px;
    border: 1px dotted #DCDFE6;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
  }
  .upload_game_img .show_img {
    max-width: 100%;
    max-height: 100%;
    border-radius: 4px;
    vertical-align: middle;
  }
</style>
